package com.example.lianjuan.ui.frament;

import android.graphics.Rect;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.lianjuan.Base.BaseFragment;
import com.example.lianjuan.Base.IBaseInfo;
import com.example.lianjuan.Model.domain.OnSellPager;
import com.example.lianjuan.Presenter.impl.impl.OnSellPresenterImpl;
import com.example.lianjuan.R;
import com.example.lianjuan.Utils.LogUtils;
import com.example.lianjuan.Utils.PresenterManang;
import com.example.lianjuan.Utils.SizeUtils;
import com.example.lianjuan.Utils.TicketUtil;
import com.example.lianjuan.Utils.ToastUtil;
import com.example.lianjuan.View.IOnSellCallback;
import com.example.lianjuan.ui.adapter.OnSellContentAdapter;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;

import java.util.List;

public class OnSellFragment extends BaseFragment implements IOnSellCallback, OnSellContentAdapter.ItemClickListener {

    private OnSellPresenterImpl mOnSellPresenter;
    private final int SuccessCode = 10000;
    private RecyclerView mOnSellListView;
    private OnSellContentAdapter mOnSellContentAdapter;
    private RefreshLayout mRefreshLayout;

    @Override
    protected void initListener() {
        super.initListener();
        mOnSellContentAdapter.setOnItemClick(this);
        //禁止下拉
        mRefreshLayout.setEnableRefresh(false);
        mRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                mOnSellPresenter.loadMore();
            }
        });
        //网络错误点击重新加载
        networkClick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnSellPresenter != null) {
                    mOnSellPresenter.reLoad();
                }
            }
        });
    }

    @Override
    protected void initPresenter() {
        super.initPresenter();
        mOnSellPresenter = PresenterManang.getInstance().getOnSellPresenter();
        mOnSellPresenter = new OnSellPresenterImpl();
        mOnSellPresenter.registerViewCallback(this);
    }

    @Override
    protected void loadData() {
        super.loadData();
        mOnSellPresenter.getOnSellContent();
    }

    @Override
    protected void initView(View view) {
        super.initView(view);
        //绑定刷新控件
        mRefreshLayout = view.findViewById(R.id.refreshLayout);
        //刷新头部显示
        mRefreshLayout.setRefreshHeader(new ClassicsHeader(getContext()));
        mRefreshLayout.setRefreshFooter(new ClassicsFooter(getContext()));
        //绑定RecyclerView
        mOnSellListView = view.findViewById(R.id.on_sell_list);
        mOnSellContentAdapter = new OnSellContentAdapter();
        //创建布局管理器
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 2);
        mOnSellListView.setLayoutManager(gridLayoutManager);
        mOnSellListView.setAdapter(mOnSellContentAdapter);
        mOnSellListView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                int topAndBottom = SizeUtils.dip2px(getContext(), 4);
                int liftAndRight = SizeUtils.dip2px(getContext(), 3);
                outRect.top = topAndBottom;
                outRect.bottom = topAndBottom;
                outRect.left = liftAndRight;
                outRect.right = liftAndRight;
            }
        });
    }

    @Override
    protected View loadRootView(LayoutInflater inflater, @Nullable ViewGroup container) {
        return inflater.inflate(R.layout.fragment_on_sell_bar_layout, container, false);
    }

    @Override
    protected int getRootViewResID() {
        return R.layout.fragment_on_sell;
    }

    @Override
    public void onLoadedSuccessContent(OnSellPager onSellContent) {
        //数据回来
        //更新UI
        if (onSellContent.getCode() == SuccessCode) {
            LogUtils.d(this, "onSellContent---->" + onSellContent.getData().getTbk_dg_optimus_material_response().getResult_list().getMap_data().get(0).getTitle());
            List<OnSellPager.DataDTO.TbkDgOptimusMaterialResponseDTO.ResultListDTO.MapDataDTO> mapData = onSellContent.getData().getTbk_dg_optimus_material_response().getResult_list().getMap_data();
            if (mapData != null) {
                mOnSellContentAdapter.setData(mapData);
            }
        }
        setUpState(State.SUCCESS);
    }

    @Override
    public void onMoreLoadedContent(OnSellPager onSellContent) {
        mRefreshLayout.finishLoadMore();
        List<OnSellPager.DataDTO.TbkDgOptimusMaterialResponseDTO.ResultListDTO.MapDataDTO> data = onSellContent.getData().getTbk_dg_optimus_material_response().getResult_list().getMap_data();
        ToastUtil.showToast("已经添加" + data.size());
        if (mOnSellContentAdapter != null) {
            mOnSellContentAdapter.setMoreData(data);
        }
    }

    @Override
    public void onMoreLoadedError() {
        mRefreshLayout.finishLoadMore();
        ToastUtil.showToast("网路异常");
    }

    @Override
    public void onMoreLoadedEmpty() {
        mRefreshLayout.finishLoadMore();
        ToastUtil.showToast("没有更多优惠");
    }

    @Override
    public void onNetworkError() {

    }

    @Override
    public void onLoadEmpty() {
        setUpState(State.EMPTY);
    }

    @Override
    public void onLoadNone() {

    }

    @Override
    public void onLoadLoading() {
        setUpState(State.LOADING);
    }

    @Override
    public void onLoadError() {
        setUpState(State.ERROR);
    }

    @Override
    protected void release() {
        super.release();
        mOnSellPresenter.unregisterViewCallback(this);
    }

    @Override
    public void onItemClick(IBaseInfo iBaseInfo) {
        TicketUtil.toTickerPager(getContext(), iBaseInfo);
    }
}
